package com.strange.mdl.module.mdl.service.debatehallcomment;

import cn.hutool.core.collection.CollUtil;
import com.strange.mdl.framework.common.util.TreeUtils;
import com.strange.mdl.module.mdl.dal.dataobject.debatehall.DebatehallUserDO;
import com.strange.mdl.module.mdl.dal.dataobject.debatehallcomment.DebateHallCommentUserDO;
import com.strange.mdl.module.mdl.dal.mysql.debatehall.DebatehallMapper;
import com.strange.mdl.module.mdl.dal.mysql.debatehallcomment.DebatehallCommentMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.strange.mdl.module.mdl.controller.admin.debatehallcomment.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.debatehallcomment.DebatehallCommentDO;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;

import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.*;

/**
 * 辨经殿话题评论 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class DebatehallCommentServiceImpl implements DebatehallCommentService {

    @Resource
    private DebatehallCommentMapper debatehallCommentMapper;

    @Resource
    private DebatehallMapper debatehallMapper;

    @Override
    public Long createDebatehallComment(DebatehallCommentSaveReqVO createReqVO) {
        // 插入
        DebatehallCommentDO debatehallComment = BeanUtils.toBean(createReqVO, DebatehallCommentDO.class);
        debatehallCommentMapper.insert(debatehallComment);
        // 返回
        return debatehallComment.getId();
    }

    @Override
    public void updateDebatehallComment(DebatehallCommentSaveReqVO updateReqVO) {
        // 校验存在
        validateDebatehallCommentExists(updateReqVO.getId());
        // 更新
        DebatehallCommentDO updateObj = BeanUtils.toBean(updateReqVO, DebatehallCommentDO.class);
        debatehallCommentMapper.updateById(updateObj);
    }

    @Override
    public void deleteDebatehallComment(Long id) {
        // 校验存在
        validateDebatehallCommentExists(id);
        // 删除
        debatehallCommentMapper.deleteById(id);
    }

    @Override
    public void deleteDebatehallCommentListByIds(List<Long> ids) {
        // 校验存在
        validateDebatehallCommentExists(ids);
        // 删除
        debatehallCommentMapper.deleteByIds(ids);
    }

    private void validateDebatehallCommentExists(List<Long> ids) {
        List<DebatehallCommentDO> list = debatehallCommentMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEBATEHALL_COMMENT_NOT_EXISTS);
        }
    }

    private void validateDebatehallCommentExists(Long id) {
        if (debatehallCommentMapper.selectById(id) == null) {
            throw exception(DEBATEHALL_COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public DebatehallCommentDO getDebatehallComment(Long id) {
        return debatehallCommentMapper.selectById(id);
    }

    @Override
    public PageResult<DebatehallCommentDO> getDebatehallCommentPage(DebatehallCommentPageReqVO pageReqVO) {
        return debatehallCommentMapper.selectPage(pageReqVO);
    }


    @Override
    public List<DebatehallCommentDO> commentByDetail(Long debateId, Long userId) {
        List<DebatehallCommentDO> debatehallCommentDOS = debatehallCommentMapper.commentByDetail(debateId, userId);
        return TreeUtils.build(debatehallCommentDOS);
    }

    @Override
    public DebatehallCommentDO insertComment(DebatehallCommentDO comment) {
        int i = debatehallCommentMapper.insertComment(comment);
        DebateHallCommentUserDO debateHallCommentUserDO = new DebateHallCommentUserDO();
        debateHallCommentUserDO.setCommentId(comment.getId());
        debateHallCommentUserDO.setUserId(comment.getUserId());
        debateHallCommentUserDO.setIsLike(0);
        debateHallCommentUserDO.setIsMessage(1);
        debatehallCommentMapper.insertCommentUsers(debateHallCommentUserDO);

        // 话题评论用户表同步
        DebatehallUserDO debatehallUserDO = debatehallMapper.selectEbatehallUsers(comment.getDebateId(), comment.getUserId());
        if (debatehallUserDO != null) {
            debatehallUserDO.setIsMessage(1);
            debatehallUserDO.setMessages(debatehallUserDO.getMessages() + 1);
            debatehallMapper.updateDebateHallUser(debatehallUserDO);
        } else {
            debatehallUserDO = new DebatehallUserDO();
            debatehallUserDO.setIsMessage(1);
            debatehallUserDO.setMessages(1);
            debatehallUserDO.setDebateId(comment.getDebateId());
            debatehallUserDO.setUserId(debatehallUserDO.getUserId());
            debatehallMapper.insertEbatehallUsers(debatehallUserDO);
        }

        DebatehallCommentDO debateHallCommentEntityResult = debatehallCommentMapper.commentById(comment.getId(), comment.getUserId());

        return debateHallCommentEntityResult;
    }

}